package gptma.fo.controler;

import gptma.bo.auth.UserRoles;
import javax.faces.context.FacesContext;
import javax.servlet.http.HttpSession;

import gptma.bo.model.employee.Employee;
import gptma.bo.model.project.Project;
import gptma.bo.model.task.Task;
import gptma.fo.aop.Current;
import java.io.Serializable;

import javax.enterprise.context.SessionScoped;
import javax.enterprise.inject.Produces;
import javax.inject.Named;
import javax.servlet.http.HttpServletRequest;

@Named("sess")
@SessionScoped
public class Session implements Serializable {

	private static final long serialVersionUID = 645837200195224771L;

	private Employee currentUser;

	private Task selectedTask;

	private Project selectedProject;

	public NavigationRule logout() {
		HttpSession session = (HttpSession) FacesContext.getCurrentInstance()
				.getExternalContext().getSession(false);
		session.invalidate();

		return NavigationRule.LOGIN;
	}

	@Produces
	@Current
	@Named
	public Employee getCurrentUser() {
		// TODO: put these annotations at field level but does not work
		// currently
		return currentUser;
	}

	public void setCurrentUser(Employee currentUser) {
		this.currentUser = currentUser;
	}

	/**
	 * @return the admin
	 */
	public boolean isAdmin() {
		HttpServletRequest req = (HttpServletRequest) FacesContext
				.getCurrentInstance().getExternalContext().getRequest();
		return req.isUserInRole(UserRoles.ROLE_ADMIN);
	}

	public Task getSelectedTask() {
		return selectedTask;
	}

	public void setSelectedTask(Task selectedTask) {
		this.selectedTask = selectedTask;
	}

	public Project getSelectedProject() {
		return selectedProject;
	}

	public void setSelectedProject(Project selectedProject) {
		this.selectedProject = selectedProject;
	}

}
